<?php 

			//创建一个验证码类
			class Captcha{
					//属性
					private $width;					//验证码图片的宽度
					private $height;					//验证码图片的高度
					private $length;					//验证码字符串的长度
					private $color;					//各种（背景色，字体，干扰线，干扰点）颜色
					private $lines;						//干扰线数量
					private $pixel;						//干扰点数量
					private $font;						//字体大小
					private $string;					//目标字符串

					/*
					 *  构造方法
					 *  @param1 $arr array() 一个一维数组
					 */
					 public function __construct($arr =array()){
						$this ->width   = isset($arr['width']) ? $arr['width'] : 80;
						$this ->height   = isset($arr['height']) ? $arr['height'] : 30;
						$this ->length   = isset($arr['length']) ? $arr['length'] : 4;
						$this ->lines   = isset($arr['lines']) ? $arr['lines'] : 4;
						$this ->pixel   = isset($arr['pixel']) ? $arr['pixel'] : 100;
						$this ->font   = isset($arr['font']) ? $arr['font'] : 5;
						$this ->string   = isset($arr['string']) ? $arr['string'] : '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
						$this ->color['bg_min']  = isset($arr['bg_min']) ? $arr['bg_min']: 200;
						$this ->color['bg_max']   = isset($arr['bg_max']) ? $arr['bg_max']: 255;
						$this ->color['font_min']  = isset($arr['font_min']) ? $arr['bg_min']: 0;
						$this ->color['font_max']   = isset($arr['font_max']) ? $arr['font_max']: 100;
						$this ->color['line_min']   = isset($arr['line_min']) ? $arr['line_min']: 120;
						$this ->color['line_max'] = isset($arr['line_max']) ? $arr['line_max']: 225;
						$this ->color['pixel_min']  = isset($arr['pixel_min']) ? $arr['pixel_min']: 120;
						$this ->color['pixel_max']  = isset($arr['pixel_max']) ? $arr['pixel_max']: 225;

					 }
					/*
					 * 获取验证码图片
					 * 
					 */
					 function generate(){
						//创建画布
						$im = imagecreatetruecolor($this->width ,$this ->height);
						//给画布添加颜色
						$bg_color=imagecolorallocate($im,mt_rand($this ->color['bg_min'],$this ->color['bg_max']),mt_rand($this ->color['bg_min'],$this ->color['bg_max']),mt_rand($this ->color['bg_min'],$this ->color['bg_max']));
						//填充画布
						imagefill($im,0,0,$bg_color);
						
						//增加干扰线
						for($i=0;$i<$this ->lines;$i++){
							//给干扰线添加颜色
							$line_color=imagecolorallocate($im,mt_rand($this->color['line_min'],$this->color['line_max']),mt_rand($this->color['line_min'],$this->color['line_max']),mt_rand($this->color['line_min'],$this->color['line_max']));
							//写入干扰线
							imageline($im,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
						}
						//增加干扰点
						for($i=0;$i<$this ->pixel;$i++){
							//给干扰点添加颜色
							$pixel_color=imagecolorallocate($im,mt_rand($this->color['pixel_min'],$this->color['pixel_max']),mt_rand($this->color['pixel_min'],$this->color['pixel_max']),mt_rand($this->color['pixel_min'],$this->color['pixel_max']));
							//写入干扰点
							imagesetpixel($im,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);
						}
						//获得随机字符串
						$captcha=$this ->getCaptchaString();
						//给字体添加颜色
						$font_color=imagecolorallocate($im,mt_rand($this->color['font_min'],$this->color['font_max']),mt_rand($this->color['font_min'],$this->color['font_max']),mt_rand($this->color['font_min'],$this->color['font_max']));
						//将字符串写入图片
						//计算写入的坐标($this->width /2) -20,($this->height /2) -20
						imagestring($im,$this->font,ceil($this->width /2) -20,ceil($this->height /2) -10,$captcha,$font_color);
						
						
						//保存输出
						imagepng($im);

						//释放资源
						imagedestroy($im);

					 }

					 /*
					  * 获取随机字符串
					  * @return $string 
					  */
					  function getCaptchaString(){
						//字符串
						$captchastr = '';
						for($i=0;$i<$this->length;$i++){
							$captchastr .= $this ->string[mt_rand(0,strlen($this->string)-1)];
							}
							//把随机字符串保存到SESSION
							$_SESSION['captcha']=$captchastr;
							return $captchastr;
					  }
					  /*
					   * 验证码的对比
					   * @return bool 成功返回TRUE，失败返回FALSE
					   */
					  public static function  checkCaptcha($captcha){
			
							return  (strtolower($captcha) === strtolower($_SESSION['captcha']));
					   }
			}

			//$img= new Captcha();
			//header('Content-type:image/png');
			//$img ->generate();
